import 'package:flutter/material.dart';
import 'package:x3_base_core/base_models/base_key_value_model.dart';
import 'package:x3_component_core/components/button_components/base_advanced_select_button.dart';
import 'package:x3_form_core/filter_components/widget/advanced_filter_expanded_icon.dart';
import 'package:x3_form_core/filter_components/widget/advanced_filter_item_container.dart';
import 'package:x3_form_core/filter_components/widget/advanced_filter_select_content.dart';
import 'package:x3_form_core/models/base_advanced_filter_model.dart';
import 'package:x3_form_core/provider/base_advanced_filter_provider.dart';

/// author：william on 2021/4/12 14:26
/// email：362661044@qq.com

mixin AdvancedFilterSelectMixin<T extends StatefulWidget> on State<T> {
  /// 全选的 key值
  final int allKey = -2;

  /// 高度
  final double rowHeight = 32;

  /// 模型
  BaseAdvancedFilterModel get model;

  /// 共享类
  BaseAdvancedFilterProvider get baseRequestFormProvider => BaseAdvancedFilterProvider.of(context, listen: false);

  /// 是否展开
  bool isExpanded = false;

  /// 选中列表
  List<BaseKeyValueModel> selectedList = [];

  List<String> get selectedListKey => selectedList.map((e) => e.key.toString()).toList();

  /// wrap 数据
  List<BaseKeyValueModel> get children;

  @override
  void initState() {
    isExpanded = model.isExpanded;

    var data = baseRequestFormProvider.getDataSource(model);
    if (data != null) {
      if (data is List) selectedList.addAll(data.map((e) => BaseKeyValueModel.fromJson(e)).toList());
      if (data is Map) selectedList.add(BaseKeyValueModel.fromJson(data));
    }

    /// 重置监听
    baseRequestFormProvider.resetStream.listen((e) {
      this.selectedList.clear();
      if (mounted) setState(() {});
    });
    initStateEvent();

    super.initState();
  }

  ///
  /// 添加初始化事件
  void initStateEvent() {}

  /// 创建 item
  Widget buildItemContainer() {
    return AdvancedFilterItemContainer(
      isRequired: model.required,
      title: model.name,
      child: _buildContent(),
      suffixChild: AdvancedFilterExpandedIcon(height: rowHeight, isExpanded: isExpanded, onExpanded: _onExpanded),
    );
  }

  /// 创建选择项
  Widget _buildContent() {
    return AdvancedFilterSelectContent(
      isExpanded: this.isExpanded,
      defaultHeight: rowHeight,
      child: Wrap(
        crossAxisAlignment: WrapCrossAlignment.start,
        spacing: 10,
        runSpacing: 8,
        children: children.map((e) {
          return BaseAdvancedSelectButton(
            isSelected: selectedListKey.contains(e.key.toString()),
            height: rowHeight,
            model: e,
            onChange: (v) => {onChange(v, e)},
          );
        }).toList(),
      ),
    );
  }

  /// 改变事件
  void onChange(bool v, BaseKeyValueModel data);

  /// 修改值
  void onChangeEvent(bool v, BaseKeyValueModel data) {
    v == true ? selectedList.add(data) : selectedList.removeWhere((element) => element.key.toString() == data.key.toString());
  }

  /// 单选修改值
  void onSingleChangeEvent(bool v, BaseKeyValueModel data) {
    if (v == true) selectedList.clear();
    onChangeEvent(v, data);
  }

  /// 拓展按钮
  void _onExpanded(bool v) {
    setState(() {
      this.isExpanded = !this.isExpanded;
    });
    this.model.isExpanded = isExpanded;
  }
}
